package controller

import (
	"ginfast/api/system/dto"
	"ginfast/api/system/model"
	"ginfast/pkg/dao"
	"ginfast/pkg/gctx"

	"github.com/gin-gonic/gin"
)

// DictType 系统管理字典类型
type DictType struct {
}

// ListHandler 字典类型列表
func (DictType) ListHandler(c *gin.Context) {
	var p dto.Where
	if err := c.ShouldBind(&p); err != nil {
		gctx.JSONWriteError(err, c)
		return
	}
	var data []model.SysDictType
	total, _ := p.DbModelWhere(&model.SysDictType{}).Find(&data)
	gctx.JSONWrite(gin.H{"total": total, "data": data}, c)
}

// GetHandler 查询字典详细
func (DictType) GetHandler(c *gin.Context) {
	dao.QueryByID(&model.SysDictType{}, c)
}

// AddHandler 新增
func (DictType) AddHandler(c *gin.Context) {
	var p model.SysDictType
	//获取参数
	if err := c.ShouldBind(&p.SysDictTypeOpt); err != nil {
		gctx.JSONWriteError(err, c)
		return
	}
	if err := dao.DbCreate(&p); err != nil {
		gctx.JSONWriteError(err, c)
		return
	}
	gctx.JSONOK(c)
}

// UpdateHandler 修改p
func (DictType) UpdateHandler(c *gin.Context) {
	var p model.SysDictType
	//获取参数
	if err := c.ShouldBind(&p.SysDictTypeOpt); err != nil {
		gctx.JSONWriteError(err, c)
		return
	}
	if err := dao.DbUpdateModel(&p); err != nil {
		gctx.JSONWriteError(err, c)
		return
	}
	gctx.JSONOK(c)
}

// DeleteHandler 删除
func (DictType) DeleteHandler(c *gin.Context) {
	dao.Deletes(&model.SysDictType{}, c)
}

func (o DictType) Router(r *gin.RouterGroup) {
	r.GET("/list", o.ListHandler)
	r.GET("/:id", o.GetHandler)
	r.POST("", o.AddHandler)
	r.PUT("", o.UpdateHandler)
	r.DELETE("/:id", o.DeleteHandler)
}
